package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.RxPermissions;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.http.api.RequestUpload;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserInfoContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.UserInfoPresenter;
import com.taomu.app.zongchouhuli.utils.BaseConst;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;
import com.taomu.app.zongchouhuli.utils.zxing.QrUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class UserInfoActivity extends BaseActivity<UserInfoContract.Presenter> implements UserInfoContract.View {

    private ImageView mBackIm;

    private LinearLayout mNickIconLin;

    private ImageView mIconIm;

    private LinearLayout mSpreadLin;

    private ImageView mSpreadIm;

    private LinearLayout mNickNameLin;

    private TextView mNickNameTv;

    private LinearLayout mGenderLin;

    private TextView mGenderTv;

    private LinearLayout mPhoneLin;

    private TextView mPhoneTv;

    private final static int MODIFY_NICK_CODE = 0x31;

    private final static int MODIFY_GENDER_CODE = 0x32;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mPresenter != null){
            mPresenter.onDetch();
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void initView() {
        mBackIm = findViewById(R.id.mine_info_back_im);
        mNickIconLin = findViewById(R.id.mine_info_icon_lin);
        mIconIm = findViewById(R.id.mine_info_icon_im);
        mSpreadLin = findViewById(R.id.mine_info_spread_lin);
        mSpreadIm = findViewById(R.id.mine_info_spread_im);
        mNickNameLin = findViewById(R.id.mine_info_nick_lin);
        mNickNameTv = findViewById(R.id.mine_info_nick_tv);
        mGenderLin = findViewById(R.id.mine_info_sex_lin);
        mGenderTv = findViewById(R.id.mine_info_sex_tv);
        mPhoneLin = findViewById(R.id.mine_info_phone_lin);
        mPhoneTv = findViewById(R.id.mine_info_phone_tv);
    }

    @Override
    protected void initData() {
        mPresenter.getUserInfo();
    }

    @Override
    protected void onResume() {
        mPresenter.getUserInfo();
        super.onResume();
    }

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mGenderLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResultsWithData(ModifyGenderActivity.class,MODIFY_GENDER_CODE,BaseConst.MODIFYGENDER,getGender());
            }
        });
        mNickNameLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResultsWithData(ModifyNickNameAcitivty.class,MODIFY_NICK_CODE,BaseConst.MODIFYNICK,getNick());
            }
        });
        mSpreadLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(UserSpreadCodeActivity.class);
            }
        });
        mNickIconLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPhoto();
            }
        });
//        mPhoneTv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startActivity(UserChangedPhoneActivity.class);
//            }
//        });
    }

    @Override
    protected UserInfoContract.Presenter createPresenter() {
        return new UserInfoPresenter(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data != null){
            if(requestCode == MODIFY_GENDER_CODE){
                String gender = data.getStringExtra(BaseConst.MODIFYGENDER);
                if("female".equals(gender)){
                    mGenderTv.setText("女");
                }else if("male".equals(gender)){
                    mGenderTv.setText("男");
                }
            }
            if(requestCode == MODIFY_NICK_CODE){
                String nick = data.getStringExtra(BaseConst.MODIFYNICK);
                mNickNameTv.setText(nick);
            }
            if(resultCode == RESULT_OK){
                if(requestCode == PictureConfig.CHOOSE_REQUEST){
                    List<LocalMedia> select = PictureSelector.obtainMultipleResult(data);
                    List<File> files = new ArrayList<>();
                    files.add(new File(select.get(0).getPath()));
                    mPresenter.uploadImg(files);
                    if(select.size()>=0){
                        Glide.with(this).load(select.get(0).getPath()).into(mIconIm);
                    }
                }
            }
        }
    }

    private String getNick(){
        return mNickNameTv.getText().toString();
    }

    private String getGender(){
        return mGenderTv.getText().toString();
    }

    private void startPhoto(){
        RxPermissions rx = new RxPermissions(this);
        rx.request(Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if(aBoolean){
                            PictureSelector.create(UserInfoActivity.this)
                                    .openGallery(PictureMimeType.ofImage())
                                    .maxSelectNum(1)
                                    .forResult(PictureConfig.CHOOSE_REQUEST);
                        }else{
                            showToast("请先允许相关权限");
                        }
                    }
                });
    }

    @Override
    public void setUserInfo(ResUserInfoEntity entity) {
        if(!TextUtils.isEmpty(entity.getUsername())){
            mNickNameTv.setText(entity.getUsername());
        }
        if(!TextUtils.isEmpty(entity.getUser_img_uri())){
            Glide.with(this).load(entity.getUser_img_uri()).into(mIconIm);
        }
        if(!TextUtils.isEmpty(entity.getSex())){
            if("0".equals(entity.getSex())) {
                mGenderTv.setText("男");
            }else if("1".equals(entity.getSex())){
                mGenderTv.setText("女");
            }
        }
        Bitmap barcodebit = QrUtils.createQr(String.valueOf(entity.getInvite_code()),48);
        if(barcodebit != null){
            mSpreadIm.setImageBitmap(barcodebit);
        }
        if(!TextUtils.isEmpty(entity.getPhone())){
            mPhoneTv.setText(entity.getPhone().substring(0,3)+"****"+entity.getPhone().substring(entity.getPhone().length()-4));
        }
    }

    @Override
    public void setImgResults(String msg) {
        showToast(msg);
    }

    @Override
    public void setPresenter(UserInfoContract.Presenter presenter) {

    }
}
